$(function(){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "json",
        url:"${baseURL}/water/user_role/getAll",
        success: function (data) {
            if(data.code == 0){
                $("#userRoleSelect").select2({
                    dropdownParent: $('#addUserForm'),
                    language: {
                        "noResults": function () {
                            return "没有数据";
                        }
                    },
                    allowClear:false,
                    placeholder: "请选择用户类型",
                    data:data.data
                });
            }else{
                swal({
                    title: "查询错误。",
                    type: "error"
                })
            }
        },
        error:function(xhr, status, error){
            console.log(xhr.responseText);
            swal({
                title: "网络错误。",
                type: "error"
            })
        }
    });

    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "json",
        url:"${baseURL}/json/user/getUserList",
        success: function (data) {
            if(data.code == 0){
                $("#userSelect").select2({
                   // dropdownParent: $('#addUserForm'),
                    language: {
                        "noResults": function () {
                            return "没有数据";
                        }
                    },
                    allowClear:false,
                    placeholder: "请选择用户",
                    data:data.data
                });
            }else{
                swal({
                    title: "查询错误。",
                    type: "error"
                })
            }
        },
        error:function(xhr, status, error){
            console.log(xhr.responseText);
            swal({
                title: "网络错误。",
                type: "error"
            })
        }
    });

});

$("#userDelBtn").click(function(){
    var id = $("#userSelect").val();
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url:"${baseURL}/json/user/delete?id=" + id,
        success: function (data) {
            if(data.code == 0){
                swal({
                    title: "用户删除成功。",
                    type: "success"
                },function(){
                    location.reload();
                })
            }else{
                swal({
                    title: "查询错误。",
                    type: "error"
                })
            }
        },
        error:function(xhr, status, error){
            console.log(xhr.responseText);
            swal({
                title: "网络错误。",
                type: "error"
            })
        }
    });
});

$("#changePasswordBtn").click(function(){
    var oldPass = $("#oldPasswordInput").val();
    var newPass = $("#newPasswordInput").val();
    var confirmPass = $("#confirmPasswordInput").val();
    if(newPass == confirmPass){
        $.ajax({
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            url: "${baseURL}/json/user/updatePassword",
            data:{"oldPass":oldPass,"newPass":newPass},
            async: false,
            success:function(data){
                if(data.code === 0){
                    swal({
                        title: "操作成功。",
                        type: "success"
                    },function(){
                        location.reload();
                    })
                }
            },
            error:function(xhr, status, error){
                console.log(xhr.responseText);
                swal({
                    title: "网络错误。",
                    type: "error"
                })
            }
        });
    }else{
        swal({
            title: "两次输入的密码不一样！",
            type: "error"
        })
    }
});

$("#pictureBtn").click(function(){
    if($("#pictureInput").val() == ""){
        swal({
            title: "没有选择头像图片。",
            type: "error"
        })
    }else{
        var form = new FormData(document.forms.namedItem("pictureForm"));
        $.ajax({
            type: "POST",
            cache: false,
            contentType:false,
            processData:false,
            url:"${baseURL}/json/user/updatePicture",
            data:form,
            success:function(data){
                if(data.code === 0){
                    swal({
                        title: "操作成功。",
                        type: "success"
                    },function(){
                        location.reload();
                    })
                }
            },
            error:function(xhr, status, error){
                console.log(xhr.responseText);
                swal({
                    title: "网络错误。",
                    type: "error"
                })
            }

        });
    }
});

$("#saveUserBtn").click(function(){
    var form = new FormData(document.forms.namedItem("addUserForm"));
    $.ajax({
        url: "${baseURL}/json/user/addUser",
        contentType: false,
        cache: false,
        processData: false,
        type: 'POST',
        dataType: 'json',
        data: form,
        success:function(data){
            if(data.code === 0) {
                swal({
                    title: "新用户添加成功！",
                    type: "success"
                }, function () {
                    location.reload();
                });
            }else{
                swal({
                    title: data.data,
                    type: "error"
                })
            }
        },
        error:function(error) {
            swal({
                title: "提交失败，请检查输入的内容。",
                type: "error"
            })
        }
    });
});